﻿using ASmile.Com;
using ASmile.Com.GridView;
using ASmile.DBModel;
using ASmile.Helpers;
using ASmile.Win.Controls;
using ASmile.Win.Extend;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ASmile.Win.Forms
{
    public partial class FrmGridColumn : FrmBase
    {
        public FrmGridColumn()
        {
            InitializeComponent();
        }

        IGridViewBLL BLLObj = ObjectFactory.GetObject<IGridViewBLL>("GridViewBLL");

        FormCrudEx<ArgGetList, Sys_GridColumn> CrudForm { get; set; }

        string OwnerName { set; get; }

        private void FrmGridColumn_Load(object sender, EventArgs e)
        {
            CrudForm = new FormCrudEx<ArgGetList, Sys_GridColumn>
            {
                BLLObj = BLLObj,
                BtnAdd = tsBtnAdd,
                BtnAddCopy = tsBtnCopyAdd,               
                BtnDel = tsBtnDelete,
                BtnEdit = tsBtnEdit,
                BtnSearch = tsBtnSearch,
                BtnADSearch = tsAdSearch,
                Dgv = dgv,
                Owner = this,
                PageNav = ucPageNav1,
                TxtSearch = tsTxtSearch,
                CreateAddForm = CreateEditForm,
                OnSearchBefore = SearchBefore
            };
            CrudForm.Init();

            if (Owner is IFormContainer dgvContainer)
            {
                OwnerName = dgvContainer.ContainerName;
                CrudForm.RefreshData();
                //BLLObj.SearchForm = dgvContainer.ContainerName;
                //SearchArg.TagData = dgvContainer.ContainerName;
                //BtnSearch_Click(sender, e);
            }
            if (IsManager)
            {
                toolStripTop.LoopItem(SetRights);
                void SetRights(ToolStripItem item)
                {
                    if (!item.Enabled) item.Enabled = item.Visible = true;
                }
            }
            dgv.SetInputEdit<Sys_GridColumn>(Sys_GridColumn.CNColumnIndex, txtColumnIndex, SetCellEdit);
            dgv.SetDropMove<Sys_GridColumn>(SwapOrderNo);
            SetCopyInsertSql<Sys_GridColumn>(dgv, Keys.P);
        }

        void SearchBefore(ArgGetList arg)
        {
            arg.FormName = OwnerName;
        }

        FrmGridColumnEdit CreateEditForm()
        {
            var frm = new FrmGridColumnEdit();
            frm.OwnerForm = Owner;
            frm.SearchFormName = OwnerName;
            frm.DgvRowCount = dgv.RowCount;
            return frm;
        }

        bool SetCellEdit(Sys_GridColumn m, EditText txt)
        {
            if (m.ColumnIndex == txtColumnIndex.Value)
            {
                return false;
            }
            var ret = BLLObj.SetColumnIndex(new ArgSetSortNo { Id = m.Id, SortNo = (int)txtColumnIndex.Value }); //m.Id, (int)txtColumnIndex.Value);
            ShowMessage(ret.msg);
            return true;
        }

        void SwapOrderNo(Sys_GridColumn srcModel, Sys_GridColumn tarModel)
        {
            ArgSwapOrderNo arg = new ArgSwapOrderNo
            {
                SrcId = srcModel.Id,
                SrcOrderNo = srcModel.ColumnIndex,
                TarId = tarModel.Id,
                TarOrderNo = tarModel.ColumnIndex
            };
            var ret = BLLObj.SwapOrderNo(arg);
            if (ret)
            {
                tarModel.ColumnIndex += srcModel.ColumnIndex;
                srcModel.ColumnIndex = tarModel.ColumnIndex - srcModel.ColumnIndex;
                tarModel.ColumnIndex = tarModel.ColumnIndex - srcModel.ColumnIndex;
                dgv.Refresh();
            }
            ShowMessage(ret.msg);
        }

        private void tsbtnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void tsBtnImport_Click(object sender, EventArgs e)
        {
            FrmGridColumnImp frm = new FrmGridColumnImp(OwnerName);
            if (frm.ShowDialog()== DialogResult.OK)
            {
                CrudForm.RefreshData();
            }
        }
    }
}